/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gps.log;

//import org.apache.log4j.Level;
import java.io.IOException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
//import org.apache.logging.log4j.core.config.ConfigurationFactory.ConfigurationSource;
//import org.apache.logging.log4j.core.config.XMLConfigurationFactory;

/**
 *
 * @author romulogalindo
 */
public class Log {

    public static int NONE = 0;
    public static int INFO = 1;
    public static int ERROR = 2;
    public static int WARN = 3;
    public static int TRACE = 4;
    public static int FATAL = 5;
    public static int DEBUG = 6;
    public static int ALL = 7;

    static private Logger log;

    public Log() {  
        try {
            Configuration config = XmlConfigurationFactory.getInstance().getConfiguration(new ConfigurationSource(getClass().getResourceAsStream("/Log4j2.xml")));
            LoggerContext ctx = (LoggerContext) LogManager.getContext(true);
            ctx.stop();
            ctx.start(config);
            
            log = ctx.getLogger("LOG1");
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(Log.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    public void setLoglevel(int level) {
        System.out.println("level:" + level);
        switch (level) {
            case 0:
                log.setLevel(Level.OFF);
                break;
            case 1:
                log.setLevel(Level.INFO);
                break;
            case 2:
                log.setLevel(Level.ERROR);
                break;
            case 3:
                log.setLevel(Level.WARN);
                break;
            case 4:
                log.setLevel(Level.TRACE);
                break;
            case 5:
                log.setLevel(Level.FATAL);
                break;
            case 6:
                log.setLevel(Level.DEBUG);
                break;
            case 7:
                log.setLevel(Level.ALL);
                break;
        }
    }

    public static void log(Object l) {
        log.info(l);
    }

    public static void log(Object l, int level) {
        switch (level) {
            case 0:
                log.info(l);
                break;
            case 1:
                log.info(l);
                break;
            case 2:
                log.error(l);
                break;
            case 3:
                log.warn(l);
                break;
            case 4:
                log.trace(l);
                break;
            case 5:
                log.fatal(l);
                break;
            case 6:
                log.debug(l);
                break;
        }
    }

}
